﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace Dao
{
    public class XSHTMXB_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(XSHTMXB entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.XSHTMXB.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(XSHTMXB entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                XSHTMXB entity2 = context.XSHTMXB.FirstOrDefault(c => c.ID == entity.ID);
                if (entity2 != null)
                {
                    //entity2 = entity;
                    context.XSHTMXB.ApplyCurrentValues(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                XSHTMXB entity = context.XSHTMXB.FirstOrDefault(c => c.ID == guid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public XSHTMXB Query(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);

                var q = from j in context.XSHTMXB
                        where j.ID == guid
                        select new
                        {
                            ID = j.ID,
                            XSHTID = j.XSHTID,
                            CPID = j.CPID,
                            SL = j.SL,
                            DJ = j.DJ,
                            ZJ=j.SL*j.DJ
                        };

                XSHTMXB entity = context.XSHTMXB.FirstOrDefault(c => c.ID == guid);
                return entity;
            }
        }

        public List<object> QueryByXSHTMXBEx(string id)
        {
            try
            {
                List<object> list = new List<object>();
                using (var context = new Dao.ErpEntities())
                {
                    Guid jid = Guid.Parse(id);
                    var q = from j in context.XSHTMXB
                            join a in context.ZL_CPSet on j.CPID equals a.ID
                            join b in context.CS_CPMCSet on a.CPXBH equals b.CPXBH                        
                            where j.XSHTID == jid&& a.CPMCBH == b.BH
                            
                            select new
                            {
                                ID = j.ID,
                                XSHTID = j.XSHTID,
                                CPID = j.CPID,
                                CPMC = b.MC,
                                SL = j.SL,
                                DJ = j.DJ,
                                ZJ = j.SL * j.DJ
                            };

                    foreach (object c in q)
                    {
                        list.Add(c);
                    }
                    return list;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<XSHTMXB> QueryAll()
        {
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.XSHTMXB
                        select c;
                List<XSHTMXB> list = new List<XSHTMXB>();
                if (q.Count() > 0)
                {
                    foreach (var c in q)
                    {
                        list.Add(c);
                    }
                }
                return list;
            }

        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<XSHTMXB> QueryBySql(string sql)
        {
            List<XSHTMXB> list = new List<XSHTMXB>();
            using (var context = new Dao.ErpEntities())
            {
                ObjectQuery<XSHTMXB> q = context.CreateQuery<XSHTMXB>(sql);
                ObjectResult<XSHTMXB> results = q.Execute(MergeOption.NoTracking);
                foreach (XSHTMXB c in results)
                {
                    list.Add(c);
                }
                return list;
            }
        }


        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<object> QueryCPAll()
        {
            using (var context = new Dao.ErpEntities())
            {               
                var query = from d in context.ZL_CPSet
                            join u in context.CS_CPMCSet
                            on d.CPXBH equals u.CPXBH   
                            where d.CPMCBH==u.BH
                            select new
                            {
                                Id = d.ID,
                                MC = u.MC
                            };
                List<object> list = new List<object>();
                if (query.Count() > 0)
                {
                    foreach (var c in query)
                    {
                        list.Add(c);
                    }
                }
                return list;
            }
        }
    }
}
